//
//  GetMoneyViewController.m
//  Xing
//
//  Created by 徐番茄丶 on 16/1/21.
//  Copyright © 2016年 徐番茄丶. All rights reserved.
//

#import "GetMoneyViewController.h"
#import "MyTools.h"
#import "ListViewController.h"
#import "AccountViewController.h"

@interface GetMoneyViewController (){
    NSDictionary *_mydic;
}
@property (weak, nonatomic) IBOutlet UILabel *jiFenLab;
@property (weak, nonatomic) IBOutlet UITextField *getField;
@property (weak, nonatomic) IBOutlet UIButton *getBtn;

@end

@implementation GetMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = [NSString stringWithFormat:@"提现到%@",_isHow];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self.navigationItem.leftBarButtonItem setTintColor:UIColorFromRGB(0xffffff)];
    
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
    self.view.backgroundColor = UIColorFromRGB(0xefeff1);
}
-(void)getDate{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic1 = @{@"user_token":[user objectForKey:@"token"]};
    
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [manager POST:CASHDES_URL parameters:dic1 success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            _mydic = [[NSDictionary alloc]initWithDictionary:[responseObject objectForKey:@"data"] ];
            [self viewCreate];
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
}
-(void)viewCreate{
    
    _getBtn.layer.cornerRadius = 5;
    
    _jiFenLab.text = [_mydic objectForKey:@"账户资金"];
    
    
    _getField.placeholder = [NSString stringWithFormat:@"提现不低于%@元",[_mydic objectForKey:@"规定不低于限额"]];
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    btn.backgroundColor = [UIColor clearColor];
    [btn addTarget:self action:@selector(fieldBack) forControlEvents:UIControlEventTouchUpInside];

    [self.view insertSubview:btn atIndex:0];
}
-(void)fieldBack{
    [_getField resignFirstResponder];
}



- (IBAction)getMoney:(id)sender {
    [self fieldBack];
    if ([[_mydic objectForKey:@"账户资金"] intValue] == 0) {
        [self.view makeToast:@"您没有可兑换的资金" duration:0.8 position:CSToastPositionCenter];
    }else if ([_getField.text intValue] > [[_mydic objectForKey:@"账户资金"] intValue]){
        [self.view makeToast:@"资金不足" duration:0.8 position:CSToastPositionCenter];
    }else{
        [self getmoney];
    }
    
}

-(void)getmoney{

    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *str;
    if ([_isHow isEqualToString:@"支付宝"]) {
        str = @"alipay_wap";
    }else if ([_isHow isEqualToString:@"微信"]){
        str = @"wx_pub";
    }
    
    NSDictionary *dic1 = @{@"user_token":[user objectForKey:@"token"],
                           @"price":_getField.text,
                           @"pay_way":str};
    
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [manager POST:CASH_URL parameters:dic1 success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            [self.view makeToast:@"提现成功，请到对应账户查看" duration:1.0 position:CSToastPositionCenter];
            ListViewController *list = [[ListViewController alloc]init];
            list.push = @"1";
            [self.navigationController pushViewController:list animated:YES];
        }else{
            //[self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
           
            UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"提示" message:[responseObject objectForKey:@"info"]preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction * aa = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if ([[responseObject objectForKey:@"info"] isEqualToString:@"请绑定支付宝"]) {
                    AccountViewController *myinfo = [[AccountViewController alloc]init];
                    [self.navigationController pushViewController:myinfo animated:YES];
                }
                
            }];
            
            [ac addAction:aa];
            [self presentViewController:ac animated:YES completion:nil];
            
        }
        NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
}


-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = YES;
    
    [self getDate];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
